
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
String.prototype.startswith = function(it) { return this.indexOf(it) == 0; };

Math.uuid = function() {
    var chars = '0123456789abcdef'.split('');
    var uuid = new Array(36), rnd=0, r;
    for (var i = 0; i < 36; i++) {
        if (i==8 || i==13 ||  i==18 || i==23) {
            uuid[i] = '-';
        } else if (i==14) {
            uuid[i] = '4';
        } else {
            if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
            r = rnd & 0xf;
            rnd = rnd >> 4;
            uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
    }
    return uuid.join('');
};

logging = {
    debug: function(text) {
        if(console) { console.log(text); }
    },
    error: function(text) {
        alert(text);
    }
};


ExecutionBuffer = function () {
    this.registry = [];
    this.executed = false;
};

ExecutionBuffer.prototype.run = function() {
    for(var i = 0; i < this.registry.length; i++) {
        this.registry[i]();
    }
    this.executed = true;
};

ExecutionBuffer.prototype.execute = function(method) {
    if(this.executed) {
        method();
    }
    else {
        this.registry.push(method);
    }
};

Initializator = function() {
	ExecutionBuffer.call(this);
    var enclosed = this;
    document.addEventListener('DOMContentLoaded', function() { enclosed.run(); }, false);
};

Initializator.prototype = new ExecutionBuffer;

Finalizator = function() {
	ExecutionBuffer.call(this);
    var enclosed = this;
    window.onbeforeunload = function() { enclosed.run(); };
};

Finalizator.prototype = new ExecutionBuffer;


init = new Initializator();
finalize = new Finalizator();

